'use strict';

var appUtil = require('../../../common/app_utils.js');
var errCodes = require('../../../common/err_code.js');
var Router = require('koa-router');
var config = require('../../../config/index.js');
var dao = require('../../../dao/face_user.js');

//
var __pathPrefix = config.baseUrl + '/v1/face/user';
appUtil.appLog('router:' + __pathPrefix);
var router = new Router({prefix: __pathPrefix});
module.exports = router.routes();

/**
 * 上传文件
 * post /api/v1/face/user/upload
 * uid=111
 * files.face=[object]
 */
router.post('/upload', function*() {
  var uid = this.request.inParams.fields.uid;
  var files = this.request.inParams.files;
  var faceFile = files && files.face;
  if (!faceFile) {
    return this.body = appUtil.ferror(errCodes.E_ARGS_INVALID, 'face文件');
  }

  var pathResult = appUtil.getPath_userFace(uid);
  appUtil.moveFile(faceFile.path, pathResult.fullPath);
  var result = yield dao.save({uid: uid, path: pathResult.fullPath, rpath: pathResult.rpath});// 保存到数据库
  this.body = appUtil.fout({
    uid: result.uid,
    ufaceid: result.ufaceid
  });
});
